package com.itheima.controller.home;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.Sms.SmsHomeAdvertiseService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsHomeAdvertise;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("home/advertise")
@Slf4j
public class HomeAdvertiseController {

    @DubboReference
    SmsHomeAdvertiseService advertiseService;


    /*
    * 1.添加广告
    * */
    @PostMapping("/create")
    public AjaxResult addCreate(@RequestBody SmsHomeAdvertise smsHomeAdvertise)
    {
        boolean save = advertiseService.save(smsHomeAdvertise);

        log.info(String.valueOf(smsHomeAdvertise.getId()));
        return AjaxResult.success(save);
    }
    /*
    * 2.删除广告
    * */
    @PostMapping("/delete")
    public AjaxResult del(@RequestParam List<Integer> ids)
    {

        boolean b = advertiseService.removeByIds(ids);
        return AjaxResult.success(b);
    }
    /*
    * 3.分页查询
    * */
    @GetMapping("/list")
public AjaxResult AllPage(@RequestParam(required = true) Integer pageNum ,@RequestParam(required = true) Integer pageSize ,@RequestParam(required = false) String endTime , @RequestParam(required = false) String name ,@RequestParam(required = false) Integer type)
{
    LambdaQueryWrapper<SmsHomeAdvertise> lambdaQueryWrapper  =new LambdaQueryWrapper<>();
    lambdaQueryWrapper.ge(endTime !=null,SmsHomeAdvertise::getEndTime, endTime);
    lambdaQueryWrapper.like(name !=null,SmsHomeAdvertise::getName, name);
    lambdaQueryWrapper.eq(type !=null,SmsHomeAdvertise::getType, type);
    TableDataInfo tableDataInfo  = new TableDataInfo();
    Page<SmsHomeAdvertise> page = new Page<>(pageNum , pageSize);
    Page<SmsHomeAdvertise> page1 = advertiseService.page(page , lambdaQueryWrapper);

    tableDataInfo.setList(page1.getRecords());

    tableDataInfo.setPageNum(pageNum);

    tableDataInfo.setPageSize(pageSize);

    tableDataInfo.setTotal(page1.getTotal());
    tableDataInfo.setTotalPage(page1.getPages());

    return AjaxResult.success(tableDataInfo);
}

    /*
    * 4.修改上下线状态
    * */
    @PostMapping("/update/status/{id}")
    public AjaxResult AlterStatus(@PathVariable Integer id , Integer status)
    {

        SmsHomeAdvertise smsHomeAdvertise = new SmsHomeAdvertise();
        smsHomeAdvertise.setStatus(status);
        smsHomeAdvertise.setId(id);
        boolean b = advertiseService.updateById(smsHomeAdvertise);

        return AjaxResult.success(b);
    }

    /*
    * 5.修改广告
    * */
    @PostMapping("/update/{id}")

    public AjaxResult  AlterAdvertise(@PathVariable Integer id , @RequestBody SmsHomeAdvertise smsHomeAdvertise)
    {

        smsHomeAdvertise.setId(id);

        boolean b = advertiseService.updateById(smsHomeAdvertise);

        return AjaxResult.success(b);
    }

    /*
    * 6.获取广告详情
    * */
    @GetMapping("{id}")
    public AjaxResult getAdvertiseText(@PathVariable Integer id)
    {

        SmsHomeAdvertise byId = advertiseService.getById(id);

        return AjaxResult.success(byId);
    }

}
